# coding: utf-8

''' 从log.log中提取统计数据, 计算QPS, 响应时间, 通过率等 
    要求: 每个进程脚本输出行需要包含total, pass, tps, resp, elapsed等关键信息

'''

from sys import argv

Log = argv[1]

Total = 0
Pass = 0

TPS = 0.0
Resp = 0.0
Threads = 0
Elapsed = 0

for ln in open(Log):
    if ln.strip():
        Threads += 1
        ln_parts = ln.split(',')

        for part in ln_parts:
            part = part.lower()
            num = part.split(":")[1].strip()
            if 'total' in part:
                Total += int(num)
            if 'pass' in part:
                Pass += int(num)
            if 'tps' in part or 'qps' in part:
                TPS += float(num)
            if 'resp' in part or 'reps' in part:
                Resp += float(num)
            if 'elapsed' in part:
                Elapsed += float(num)

print '-' * 30
print "      Total: %d, Pass: %d, Fail: %d" % (Total, Pass, Total-Pass)
print "      Ratio: %.2f%%" % (float(Pass) / Total * 100)
print "        TPS: %.2f hits/s" % TPS
print "  Resp_time: %.1f ms" % (Resp*1000 / Threads)
print "    Elapsed: %.2f s" % (Elapsed / Threads)
print "Concurrency: %d thread(s)" % Threads 

